import { createSlice } from '@reduxjs/toolkit';

const sessionSlice = createSlice({
	name: 'session',
	initialState: {
		customerClientId: '',
		agentClientId: '',
		members: [],
		bizScenarioId: '',
		group: '',
		invitationId: '',
		roomId: '',
		sessionId: '',
		userData: {
			fullName: '',
			idCard: ''
		},
		customer: {},
		publicationId: ''
	},
	reducers: {
		setSessionData(state, action) {
			return { ...state, ...action.payload };
		},
		setSessionMembers(state, action) {
			return { ...state.members, ...action.payload };
		},
		setJoinMembers(state, action) {
			let members: any = [...state.members, action.payload];
			return { ...state, members };
		},
		setMembersLeave(state, action) {
			let members = state.members.filter((item: any) => item.id !== action.payload);
			return { ...state, members };
		},
		clearSessionData() {
			return {
				customerClientId: '',
				agentClientId: '',
				members: [],
				bizScenarioId: '',
				group: '',
				invitationId: '',
				roomId: '',
				sessionId: '',
				userData: {
					fullName: '',
					userName: '',
					idCard: ''
				},
				customer: {},
				receiveMediaType: '',
				publicationId: ''
			};
		}
	}
});

export const { setSessionData, setSessionMembers, clearSessionData, setJoinMembers, setMembersLeave } = sessionSlice.actions;
export default sessionSlice.reducer;
